package com.goalias.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.goalias.constant.SystemConstants;
import com.goalias.domain.entity.Comment;
import com.goalias.domain.vo.CommentVo;
import com.goalias.exception.ExceptionEnum;
import com.goalias.feign.UserClient;
import com.goalias.feign.domain.User;
import com.goalias.feign.domain.UserInfo;
import com.goalias.mapper.CommentMapper;
import com.goalias.resp.PageResult;
import com.goalias.resp.Result;
import com.goalias.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.goalias.utils.IoUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author goalias
 * @since 2024-12-12
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Resource
    private UserClient userService;

    @Resource
    private IoUtil iOUtil;

    @Override
    public Result getCommentList(Long articleId, Integer pageNum, Integer pageSize) {//
        // 查询所有评论
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getArticleId, articleId);
        Page<Comment> page = new Page<>(pageNum, pageSize);
        this.page(page, wrapper);

        List<Comment> comments = page.getRecords();

        // 为评论添加用户信息
        List<CommentVo> commentVos = BeanUtil.copyToList(comments, CommentVo.class);
        for (CommentVo commentVo : commentVos) {
            User user = userService.info(commentVo.getCreateBy());
            if (ObjectUtil.isEmpty(user)) {
                continue;
            }
            commentVo.setUserName(user.getNickName());
            commentVo.setIsAdmin(SystemConstants.ADMIN_USER.equals(user.getType()));
            commentVo.setAvatar(StringUtils.hasText(user.getAvatar()) ?
                    iOUtil.getUrl(iOUtil.getBUCKET_NAME(), user.getAvatar())
                    : iOUtil.getUrl(iOUtil.getBUCKET_NAME(), iOUtil.DEFAULT_AVATAR()));
        }
        // 查询所有子评论
        return Result.success(new PageResult<>(commentVos, (long) commentVos.size(), page.getTotal()));
    }

    @Override
    public Result addComment(Comment comment) {
        UserInfo userInfo = UserInfo.get();
        if (ObjectUtil.isNull(userInfo)) {
            return Result.error(ExceptionEnum.NEED_LOGIN);
        }
        comment.setCreateBy(userInfo.getId());
        save(comment);
        return Result.success();
    }

    @Override
    public Result updateComment(Comment comment) {
        boolean success = updateById(comment);
        if (!success) {
            return Result.error(ExceptionEnum.DATABASE_ERROR);
        }
        return Result.success();
    }

    @Override
    public Result deleteCommand(Long id) {
        boolean success = removeById(id);
        if (!success) {
            return Result.error(ExceptionEnum.DATABASE_ERROR);
        }
        return Result.success();
    }
}
